<?php
    namespace lactone\Core\Queue\Driver;

    class ArrayDriver implements QueueDriverInterface{

        protected $lock;                //队列锁

        protected $Queue;               //队列数据集合

        public $pushNum  = 0;           //写入总次数

        public $shiftNum = 0;           //读取总次数

        public function __construct(){
            $this->Queue = [];

            $this->lock  = new \swoole_lock();

        }

        public function push($data){
            $this->lock->lock();
            array_push($this->Queue,$data);
            $this->pushNum++;
            $this->lock->unlock();
            return true;
        }

        public function shift(){
            $this->lock->lock();
            $result = array_shift($this->Queue);
            $this->shiftNum++;
            $this->lock->unlock();
            return $result;
        }

        public function isEmpty(){
            $this->lock->lock();
            $result = empty($this->Queue);
            $this->lock->unlock();
            return $result;
        }

        public function count(){
            $this->lock->lock();
            $result = count($this->Queue);
            $this->lock->unlock();
            return $result;
        }

        public function initConfig($Configs){
            return true;
        }

        public function reset(){
            return true;
        }
    }